package net.oschina.dajiangnan.wx.process.msg;

import net.oschina.dajiangnan.util.XStreamHelper;
import org.apache.log4j.Logger;

import net.oschina.dajiangnan.wx.constants.Custom;
import net.oschina.dajiangnan.wx.process.msg.xmlbean.ReceiveText;
import net.oschina.dajiangnan.wx.process.msg.xmlbean.SendNews;
import net.oschina.dajiangnan.wx.process.msg.xmlbean.SendText;
import net.oschina.dajiangnan.wx.process.msg.xmlbean.XmlBean;
import net.oschina.dajiangnan.wx.process.msg.xmlbean.newsItems.Items;


public class Text {

    private static Logger logger = Logger.getLogger(Text.class);
    
    public XmlBean process(String xmlString){
        
        ReceiveText receiveText = XStreamHelper.toBean(xmlString, ReceiveText.class);
        
        logger.debug("接收消息内容如下 : \r\n" + receiveText);
        
        
        XmlBean respXmlBean = null;
        
        
        if(respXmlBean == null){
            String respText = Custom.wx.sendTextFromText(receiveText.getFromUserName(),receiveText.getContent());
            if(respText != null){
                respXmlBean = new SendText();
                ((SendText)respXmlBean).setMsgType("text");
                ((SendText)respXmlBean).setCreateTime(receiveText.getCreateTime());
                ((SendText)respXmlBean).setFromUserName(receiveText.getToUserName());
                ((SendText)respXmlBean).setToUserName(receiveText.getFromUserName());
                ((SendText)respXmlBean).setContent(respText);
            }
        }
        if(respXmlBean == null){
            Items items = Custom.wx.sendNewsFromText(receiveText.getFromUserName(),receiveText.getContent());
            if(items != null){
                respXmlBean = new SendNews();
                ((SendNews)respXmlBean).setCreateTime(receiveText.getCreateTime());
                ((SendNews)respXmlBean).setFromUserName(receiveText.getToUserName());
                ((SendNews)respXmlBean).setToUserName(receiveText.getFromUserName());
                ((SendNews)respXmlBean).setMsgType("news");
                ((SendNews)respXmlBean).setArticleCount(items.getItem().size());
                ((SendNews)respXmlBean).setItems(items);
            }
        }
        
        logger.debug("接收消息应答内容如下\r\n" + respXmlBean);
        
        return respXmlBean;
    }
}
